<template>
  <div class="table1">
    <div class="btn-wrap">
      <a-button type="primary" style="margin-right: 15px;" @click="addFormVisible = true">添加</a-button>
      <a-button type="info" @click="download">导出</a-button>
    </div>
    <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="true" :loading="loading" :columns="columns"
      :dataSource="pageData" @selectedRow="onSelectedRow" @change="onTableChange">
      <!--操作-->
      <template slot="action" slot-scope="text, record">
        <a-button @click="onDetail(record)" type="link" style="padding:0;">查看</a-button>
      </template>
      <template slot="approvalStatus" slot-scope="text, record">
        <span v-if="text == 1" style="color: blue">审批中</span>
        <span v-if="text == 2" style="color: green">已通过</span>
        <span v-if="text == 3" style="color: red">未通过</span>
      </template>
    </z-table>
    <a-modal title="添加" :visible="addFormVisible" @ok="onAddSubmit" @cancel="onAddFormCancel">
      <a-form-model ref="addForm" :model="addForm" :label-col="{span: 7}" :wrapper-col="{span: 16}">
          <a-form-model-item label="组员名称" prop="memberId" :rules="{required: true, message: '请选择组员', trigger: ['change']}">
            <a-select v-model="addForm.memberId">
              <a-select-option :value="item.id" v-for="item in memberList" :key="id">{{item.memberName}}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="小组观察日标" prop="observationLog" :rules="{required: true, message: '请输入小组观察日标', trigger: ['change']}">
            <a-textarea v-model="addForm.observationLog" :auto-size="{ minRows: 10, maxRows: 10 }"></a-textarea>
          </a-form-model-item>
          <a-form-model-item label="小组观察情况评估" prop="observationEvaluation" :rules="{required: true, message: '请输入小组观察情况评估', trigger: ['change']}">
            <a-textarea v-model="addForm.observationEvaluation" :auto-size="{ minRows: 10, maxRows: 10 }"></a-textarea>
          </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
  import { memberList } from '@/api/sjsk/activity/group/detail/mature/table1'
  import { getList, add, update } from '@/api/sjsk/activity/group/detail/mature/table4'
  import downloadXlsl from '../../downloadXlsx.js'
  export default {
    data() {
      return {
        loading:false,
        pageFilter: {
          pageNo: 1,
          pageSize: 10
        },
        pageData: {},
        form: {},
        headerData: [],
        memberList: [],
        columns: [
          {
              title: '序号',
              
              width: 55,
              align: 'center',
              customRender: (text,record,index) => {
                return index + 1
              }
          },
          {
            title: '组员名称',
            dataIndex: 'memberName',
            width: 80,
            align: 'center',
            customRender: text => {
              return <j-ellipsis value={text} length={14} />
            }
        },
        {
            title: '组内职责',
            dataIndex: 'memberIdentity',
            width: 80,
            align: 'center',
            customRender: text => {
              return <j-ellipsis value={text == 1 ? '组员' : '志愿者'} length={14} />
            }
        },
        {
            title: '提交情况',
            dataIndex: 'submitStatus',
            width: 80,
            align: 'center',
            customRender: text => {
              let status = ''
              switch (text){
                case 1:
                status = '已提交'
                  break;
                case 2:
                status = '待审批'
                  break;
                case 3:
                status = '已通过'
                  break;
                case 4:
                status = '未通过'
                  break;
              }
              return <j-ellipsis value={status} length={14} />
            }
        },
        {
            title: '提交时间',
            dataIndex: 'submitDate',
            width: 80,
            align: 'center',
            customRender: text => {
              return <j-ellipsis value={text} length={14} />
            }
        },
        // {
        //     title: '审批状态',
        //     dataIndex: 'approvalStatus',
        //     width: 80,
        //     align: 'center',
        //     scopedSlots: { customRender: 'approvalStatus' }
        // },
        // {
        //     title: '审批时间',
        //     dataIndex: 'approvalDate',
        //     width: 80,
        //     align: 'center',
        //     customRender: text => {
        //       return <j-ellipsis value={text} length={14} />
        //     }
        // },
        {
          title: '操作',
          width: 120,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
        ],
        addFormVisible: false,
        addForm: {
            "activityId": '',
            "observationLog": "",
            "memberId": '',
            observationEvaluation: ''
        }
      }
    },
    created() {
      this.addForm.activityId = this.$route.query.id
      this.getMemberList()
      this.onQuery()
    },
    
    methods: {
      download() {
        downloadXlsl(this.$route.query.id, 7)
      },
      getMemberList() {
        memberList({activityId: this.$route.query.id}).then(res => {
          if(res.success) {
            this.memberList = res.result.records
          }
        })
      },
      onQuery() {
        this.pageFilter.pageNo = 1
        this.refreshTable()
      },
      onSelectedRow(keys) {
        this.selectKeys = keys
        this.isDisabled = keys.length > 0 ? false : true
      },
      onTableChange(pagination, pageFilter, sorter) {
        let keyMap = {
          currentPage: 'pageNo'
        }
        let paginations = Object.keys(pagination).reduce((newData, key) => {
          let newKey = keyMap[key] || key
          newData[newKey] = pagination[key]
          return newData
        }, {})
        this.pageFilter = Object.assign({}, this.pageFilter, paginations)
        this.refreshTable()
      },
      refreshTable() {
        let form = Object.assign({}, this.queryParam, this.pageFilter)
        form.activityId = this.$route.query.id
        this.loading = true
        getList(form).then((res) => {
            if (res.success) {
              this.pageData = res.result.record
              
              this.loading = false
              this.$refs.table.onClearRowKeys()
            } else {
              this.loading = false
            }
          })
          .catch((e) => {
            this.loading = false
            this.$message.warn('请求失败！')
          })
      },
      onAddSubmit() {
        this.$refs.addForm.validate().then(() => {
          add(this.addForm).then(res => {
            this.$message.success('添加成功！')
            this.addFormVisible = false
          })
        }).catch(() => {
          this.$message.success('请完整填写表单后，再进行提交！')
        })
      },
      onAddFormCancel() {
        this.addFormVisible = false
        this.addForm = {
            "activityId": this.$route.query.id,
            "observationLog": "",
            "memberId": '',
            observationEvaluation: ''
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .table1 {
    box-sizing: border-box;
    padding: 20px 30px;
    .btn-wrap{
      margin-bottom: 20px;
    }
  }
</style>
